package waal.pc.frontend;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToolBar;

import waal.pc.backend.hub.Hub;
import waal.pc.backend.hub.HubEvent;
import waal.pc.backend.hub.HubEventListener;

public class ToolBar implements HubEventListener {

	private Surface surface;
	private WaalActions actions;
	private JToolBar toolBar;
	private Map<Hub,Integer> hubActions;
	
	public ToolBar(Surface surface) {
		this.surface = surface;
		this.actions = this.surface.getWaalActions();
		this.toolBar = new JToolBar();
		this.toolBar.setPreferredSize( new Dimension(200,50));
		this.hubActions = new HashMap<Hub, Integer>();
		this.surface.addHubEventListener(this);
	}
	
	public JToolBar getToolBar() {
		return this.toolBar;
	}


	@Override
	public void actionHubEvent(HubEvent e) {
		int i;
		JButton b;
		switch(e.getEvent()) {
		case 1: // new Hub
			b = new JButton();
			JLabel l = new JLabel("Hub " + e.getHub().getNumber());
			if(e.getHub().isMeasuring()) {
				b.setAction(this.actions.getActionForHub(e.getHub(), WaalActions.STOPMEASURMENT));
			} else {
				b.setAction(this.actions.getActionForHub(e.getHub(), WaalActions.STARTMEASURMENT));
			}
			this.hubActions.put(e.getHub(), this.toolBar.getComponentCount());
			this.toolBar.add(l);
			this.toolBar.add(b);
			this.toolBar.addSeparator();
			break;
		case 2: // Hub remove
			if(this.hubActions.containsKey(e.getHub())) {
				
				i = this.hubActions.get(e.getHub());
				//System.out.println("Number: " + i + " of: " + this.toolBar.getComponentCount());
				this.toolBar.remove(i);
				//System.out.println("Number: " + i + " of: " + this.toolBar.getComponentCount());
				this.toolBar.remove(i);
				//System.out.println("Number: " + i + " of: " + this.toolBar.getComponentCount());
				this.toolBar.remove(i);
				//System.out.println("Number: " + i + " of: " + this.toolBar.getComponentCount());
				for(Map.Entry<Hub, Integer> entry : this.hubActions.entrySet()) {
					if(entry.getValue() != null) {
						this.hubActions.put(entry.getKey(), (entry.getValue()-3));
					}
				}				
			}
			break;
		case 3: // Measurment start
			if(this.hubActions.containsKey(e.getHub())) {
				i = this.hubActions.get(e.getHub());
				if( this.toolBar.getComponent(i+1) instanceof JButton ) {
					b = ((JButton) this.toolBar.getComponent(i+1));
					b.setAction(this.actions.getActionForHub(e.getHub(), WaalActions.STOPMEASURMENT));
				} 
			}
			break;	
		case 4:// Measurment stop
			if(this.hubActions.containsKey(e.getHub())) {
				i = this.hubActions.get(e.getHub());
				System.out.println("Number: " + i + " of: " + this.toolBar.getComponentCount());
				if( this.toolBar.getComponent(i+1) instanceof JButton ) {
					b = ((JButton) this.toolBar.getComponent(i+1));
					b.setAction(this.actions.getActionForHub(e.getHub(), WaalActions.STARTMEASURMENT));
				} 
			}
			break;
		}
		this.toolBar.repaint();
	}
}
